let mongoose = require('mongoose');
let Common = require('../../util/common')

let schema = new mongoose.Schema({
    signTimeTick:{
        type:Number,
        default:0
    },
    currentDate:{
        type:Number,
        default:0
    },
    signCount:{
        type:Number,
        default:0
    }
});

schema.methods={
    //type:1 钻石  2金币  101飞镖
    getRewards(options){
        if(this.signCount<7){
            return {
                1:[{type:2,count:50}],
                2:[{type:1,count:80}],
                3:[{type:options.self.sex==1?304:404,count:1}],
                4:[{type:101,count:3}],
                5:[{type:1,count:80},{type:2,count:100}],
                6:[{type:1,count:120},{type:2,count:200}],
                7:[{type:1,count:150},{type:2,count:200},{type:101,count:5}],
            }
        }else{
            return {
                1:[{type:2,count:50}],
                2:[{type:1,count:80}],
                3:[{type:101,count:3}],
                4:[{type:1,count:80},{type:2,count:100}],
                5:[{type:1,count:120},{type:2,count:200}],
                6:[{type:1,count:150},{type:2,count:200}],
                7:[{type:1,count:150},{type:2,count:200},{type:101,count:5}],
            }
        }

    },
    getMessage(options){
        return {
            currentDate:this.getCurrentDate(),
            hasSign:this.hasSign(),
            rewards:this.getRewards(options)
        }
    },
    getCurrentDate(){
        let now=new Date();
        let signTime=new Date(this.signTimeTick);
        let currentDate=1;
        let dtDay = Common.calcDetailDay(signTime,now);
        if(dtDay==0){
            currentDate=this.currentDate;
        } else if(dtDay == 1){
            currentDate=this.currentDate+1;
            if(currentDate===8){
                currentDate=1;
            }
        }
        // if(now.getFullYear()==signTime.getFullYear()&&now.getMonth()==signTime.getMonth()&&now.getDay()==signTime.getDay()){
        //     currentDate=this.currentDate;
        // }
        // if(now.getFullYear()==signTime.getFullYear()&&now.getMonth()==signTime.getMonth()&&now.getDay()-signTime.getDay()===1){
        //     currentDate=this.currentDate+1;
        //     if(currentDate===8){
        //         currentDate=1;
        //     }
        // }
        return currentDate;
    },
    hasSign(){
        let now=new Date();
        let signTime=new Date(this.signTimeTick);
        let hasSign=true;
        if(now.toLocaleDateString()==signTime.toLocaleDateString()){
            hasSign=false;
        }
        return hasSign;
    },
    sign(options){
        if(!this.hasSign()){
           throw new Error('请勿重复签到。');
        }
        return this._rewarded(options.self,1);
    },

    signDouble(options){
        if(!this.hasSign()){
            throw new Error('请勿重复签到。');
        }
        return this._rewarded(options.self,2);
    },
    //获得奖励
    _rewarded(gameUser,multiple=1){
        let currentDate=this.getCurrentDate();
        let rewards=this.getRewards({self:gameUser});
        let reward=rewards[currentDate];
        for(let i=0;i<reward.length;i++){
            reward[i].count*=multiple;
            // switch(reward[i].type){
            //     case 1:{  //钻石
            //         gameUser.store.diamond+=reward[i].count;
            //         break;
            //     }
            //     case 2:{ //金币
            //         gameUser.store.goldCoin+=reward[i].count;
            //         break;
            //     }
            //     case 101:{ //飞镖
            //         gameUser.bag.addItem(101,reward[i].count);
            //         break;
            //     }
            // }
        }
        gameUser.getGoods(reward);
        this.signTimeTick=new Date().getTime();
        this.currentDate=currentDate;
        //签到后完成签到任务
        gameUser.mission.doOneMission(1);
        this.signCount++;
        return reward;
    },
    getHint(){
        return this.hasSign() ? 1 : 0
    }
};

module.exports=schema;